package cn.iocoder.yudao.module.wgxm.controller.admin.lx;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.wgxm.controller.admin.lx.vo.*;
import cn.iocoder.yudao.module.wgxm.dal.dataobject.lx.LxDO;
import cn.iocoder.yudao.module.wgxm.service.lx.LxService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 项目立项")
@RestController
@RequestMapping("/wgxm/lx")
@Validated
public class LxController {

    @Resource
    private LxService lxService;


    //发起流程
    @PostMapping("/createPro")
    @Operation(summary = "发起审批流")
    public CommonResult<Long> createLxProc(@Valid @RequestBody LxSaveReqVO createReqVO) {
        return success(lxService.createLxProc(getLoginUserId(), createReqVO));
    }

    @PostMapping("/create")
    @Operation(summary = "创建项目立项")
    @PreAuthorize("@ss.hasPermission('wgxm:lx:create')")
    public CommonResult<Long> createLx(@Valid @RequestBody LxSaveReqVO createReqVO) {
        return success(lxService.createLx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目立项")
    @PreAuthorize("@ss.hasPermission('wgxm:lx:update')")
    public CommonResult<Boolean> updateLx(@Valid @RequestBody LxSaveReqVO updateReqVO) {
        lxService.updateLx(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目立项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wgxm:lx:delete')")
    public CommonResult<Boolean> deleteLx(@RequestParam("id") Long id) {
        lxService.deleteLx(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目立项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wgxm:lx:query')")
    public CommonResult<LxRespVO> getLx(@RequestParam("id") Long id) {
        LxDO lx = lxService.getLx(id);
        return success(BeanUtils.toBean(lx, LxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目立项分页")
    @PreAuthorize("@ss.hasPermission('wgxm:lx:query')")
    public CommonResult<PageResult<LxRespVO>> getLxPage(@Valid LxPageReqVO pageReqVO) {
        PageResult<LxDO> pageResult = lxService.getLxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目立项 Excel")
    @PreAuthorize("@ss.hasPermission('wgxm:lx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLxExcel(@Valid LxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LxDO> list = lxService.getLxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目立项.xls", "数据", LxRespVO.class,
                        BeanUtils.toBean(list, LxRespVO.class));
    }

}